package app.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@WebFilter(
	filterName="AppFilter",urlPatterns={"/yourapp/*"},
	initParams={@WebInitParam(name="enabled", value="false")}
)
public class YourAppFilter implements Filter {
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(YourAppFilter.class);

    private static boolean enabled;

    public void init(FilterConfig filterConfig) throws ServletException {
        enabled = Boolean.valueOf(filterConfig.getInitParameter("enabled"));
        logger.info("captcha enabled:{}",enabled);
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
            ServletException {
    	HttpServletRequest request = (HttpServletRequest) req;
//    	if(!request.getHeader("auth_token").equals("app")){
//    	    return;
//    	}
        logger.info("~~ filter context:{},method:{} ~",request.getRequestURI(),request.getMethod());
        // 判断是否启用验证码功能
        if (!enabled) {
            chain.doFilter(req, res);
            return;
        }
        logger.info("~~ doFilter ~~");
        chain.doFilter(req, res);
    }

    @Override
    public void destroy() {
    }
}